package com.mths.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.Widget;
import com.mths.client.component.Button;
import com.mths.client.util.UrlUtil;

public class UploadFile extends Composite {

	private static UploadFileUiBinder	uiBinder	= GWT.create(UploadFileUiBinder.class);

	interface UploadFileUiBinder extends UiBinder<Widget, UploadFile> {
	}

	@UiField
	FormPanel		uploadFileForm;
	@UiField
	FileUpload		fileUpload;
	@UiField
	Button			uploadBtn;

	private String	fileSessionKey	= System.currentTimeMillis() + "";

	public UploadFile() {
		initWidget(uiBinder.createAndBindUi(this));

		fileUpload.setName("fileUpload");

		uploadFileForm.setAction(UrlUtil.generateFileUploadUrl(fileSessionKey));
		uploadFileForm.setEncoding(FormPanel.ENCODING_MULTIPART);
		uploadFileForm.setMethod(FormPanel.METHOD_POST);

		uploadFileForm.addSubmitHandler(new SubmitHandler() {
			@Override
			public void onSubmit(SubmitEvent event) {
				if (!controlFileBeforeSubmit(event)) {
					event.cancel();
				}
			}
		});

		uploadBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				uploadFileForm.submit();
			}
		});
	}

	public HandlerRegistration addCompletedCallback(final AsyncCallback<SubmitCompleteEvent> callback) {
		return uploadFileForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				callback.onSuccess(event);
			}
		});
	}

	public HandlerRegistration addFileChangedCallback(final AsyncCallback<ChangeEvent> callback) {
		return fileUpload.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				callback.onSuccess(event);
			}
		});
	}

	private boolean controlFileBeforeSubmit(SubmitEvent event) {
		if ("".equals(fileUpload.getFilename())) {
			Window.alert("No file selected");
			return false;
		}
		return true;
	}

	public String getFileSessionKey() {
		return fileSessionKey;
	}

	public void setFileSessionKey(String fileSessionKey) {
		this.fileSessionKey = fileSessionKey;
	}

	public FormPanel getUploadFileForm() {
		return uploadFileForm;
	}
}
